(function($){  
	
	$.st_tooltip = function(node, options){
		
		var defaults = {    
			    content:'',
			    width: 300,    
			    height: 200,
			    location:{},
			    inteval:5
			  };
		
		options = $.extend(defaults, options || {});
		var that = this;
		var tooltipDiv;
		that.init = function(){
			that.node = $(node);
			that.content = options.content;
			that.width = options.width;
			that.inteval = options.inteval;
			that.height = options.height;
			that.location = $.extend(that.calculateLocation(),options.location);
			that.bind();
		};
		
		that.calculateLocation = function(){
			
			var newLocationX = 0;
			var newLocationY = 0;
			var locationX = that.node.offset().top;
			var locationY = that.node.offset().left;
		  
			var elHeight = that.node.height();
			var elWidth = that.node.width();
		  
			var winHeight = $(window).height();
		
			newLocationY = locationY - that.inteval - that.width;
			newLocationX = locationX + that.inteval + elHeight;
		
		    if(newLocationY < 0){
		    	newLocationY = locationY + that.inteval + elWidth;
		    }
	
		    if(newLocationX > winHeight - that.height ){
		    	newLocationX = locationX - that.inteval - that.height;
		    }
		    
		    return {'top':newLocationX,'left':newLocationY};
		};
		
		var hasHide = true;
		
		that.generateTooltip = function(){
			var tooltipDiv = $("<div></div>").css("border","1px solid #2B78E4").css("width",that.width).css("height",that.height)
				.css("background-color","#FCF7B6").css("position","absolute").css("top",that.location.top).css("left",that.location.left).css("z-index","9999").css("display","none").addClass("round");
			
			var _1 = $("<div></div>").css("height","24").css("width",that.width-2).css("padding","2").css("background-color","#E8F1FF").addClass("round");
			var _11 = $("<div></div>").css("font-weight","600").css("color","#0E2D5F").css("margin-left","3px").css("float","left").text("提示");
			var _12 = $("<div></div>").css("margin-right","3px").css("float","right").bind("click",function(){hasHide = true;tooltipDiv.remove();});
			var _121 = $("<i></i>").addClass("icon-remove").addClass("icon-large").css("color","black").css("cursor","pointer");
			var _2 = $("<div></div>").css("padding","5").css("background-color","#FCF7B6").css("border-top","1px solid #2B78E4").text(that.content);
			
			tooltipDiv.append(_1.append(_11).append(_12.append(_121))).append(_2);
			
			tooltipDiv.appendTo('body');
			
			return tooltipDiv;
		};
		
		
		that.bind = function(){
			
			that.node.bind({
				click:function(){
					hasHide = false;
				},
				mouseover:function(){
					that.tooltipDiv = that.generateTooltip();
					$(that.tooltipDiv).fadeIn(500);
				},
				mouseout:function(){
					if(hasHide){
						$(that.tooltipDiv).fadeOut(2000);
						setTimeout(function(){
							if(hasHide){
								$(that.tooltipDiv).remove();
							}
			            },2100);
					}
				}
			})
		};
		that.init();
	};
	
	$.fn.st_tooltip = function(options){
		return this.each(function(i){
			$.st_tooltip(this,options);
		});
	};
	
	$.fn.createDraggle = function(options){
		
		var defaults = {    
			   splitHeight:35,
			   splitPercent:0.6,
			   bufferHeight:5
			  };
		
		options = jQuery.extend(defaults, options || {});
		
		var node = $(this);
		var subNode = node.children();
		var header = subNode.eq(0);
		var split = subNode.eq(1);
		var footer = subNode.eq(2);
		
		var allHeight = node.height();
		
		var totalHeight = allHeight-options.splitHeight-options.bufferHeight;
		
		var headerHight = totalHeight * options.splitPercent;
		var footerHight = totalHeight * (1-options.splitPercent);
		header.height(headerHight);
		footer.height(footerHight);
		
		var splitTools = split.find("a");
		var up = splitTools.eq(0);			
		var middle = splitTools.eq(1);			
		var down = splitTools.eq(2);
		
		up.click(function(){
			header.height(0);
			header.css("overflow","hidden");
			footer.height(totalHeight);
		});
		
		down.click(function(){
			header.height(totalHeight);
			footer.height(0);
			footer.css("overflow","hidden");
		});
		
		var initY = 0;
		var dragging_up = false;
		var dragging_down = false;
		
		var newHeaderHeight = 0;
		var newFooterPanel = 0; 
		
		middle.mousedown(function(event){
			dragging_up = true;
			dragging_down = true;
			initY = event.pageY;
			this.setCapture && this.setCapture();
			headerHight = header.height();
			footerHight = footer.height();
		});
		
		
		middle.mousemove(function(event){
			var momentY = event.pageY - initY;
			if(dragging_up && momentY < 0){
				newHeaderHeight = headerHight+momentY;
				newFooterPanel = footerHight-momentY;
				if(newFooterPanel + options.bufferHeight > totalHeight){
					dragging_up = false;
					dragging_down = true;
				}
				header.height(newHeaderHeight);
				footer.height(newFooterPanel);
			}
			if(dragging_down && momentY > 0){
				newHeaderHeight = headerHight+momentY;
				newFooterPanel = footerHight-momentY;
				if(newHeaderHeight + options.bufferHeight > totalHeight){
					dragging_down = false;
					dragging_up = true;
				}
				header.height(newHeaderHeight);
				footer.height(newFooterPanel);
				footer.css("overflow","hidden");
			}
		});
		
		middle.mouseup(function(event){
			dragging_up = false;
			dragging_down = false;
			event.cancelBubble = true;
		});
		
		
	};
	
	
	$.openNewWindow = function(options){
		
		var defaults = {
			url : "",
			param : {},
			rollbackMethodName:"",
			rollbackMethodParams:[]
		};
		
		options = $.extend(defaults, options || {});
		var id = new Date().getTime();
		var url = options.url;
		var param = options.param;
		if(url == null || url == ""){
			$.messager.alert("请求URL地址不能为空!");
			return;
		}
		var _divnew = $("<div></div>").attr("id",id).addClass("hidden");
		var _form = $("<form></form>").attr("action",url).attr("target","_blank").attr("method","post");
		for(var key in param){
			var _input = $("<input></input>").attr("name",key).attr("value",param[key]).attr("type","hidden");
			_form.append(_input);
		}
		
		if(options.rollbackMethodName != null && options.rollbackMethodName != ""){
			var _input = $("<input></input>").attr("name","rollbackMethodName").attr("value",options.rollbackMethodName).attr("type","hidden");
			_form.append(_input);
		}
		
		if(options.rollbackMethodParams != null && options.rollbackMethodParams != ""){
			var _input = $("<input></input>").attr("name","rollbackMethodParams").attr("value",options.rollbackMethodParams.join(",")).attr("type","hidden");
			_form.append(_input);
		}
		$("body").append(_divnew.append(_form));
		_form.submit();
		var _div = $("#"+id);
		if(_div != null){
			_div.remove();
		}
	};
	
	
	$.fn.selectRegister = function(){
		
		var ul = $(this).parent().find("ul.select-con");
		$(this).bind("click",function(){
			var visible = ul.is(":visible");
			if(visible){
				ul.hide();
			}else{
				ul.show();
			}
		});
		
	};
	
	
	
	$.ajaxWithShade = function(options){
		
		var defaults = {    
			timeout : 60000,
			async : true,
			cache : false,
			shadeModel : "default",
			error : function(){},
			success : function(){}
		};
			  
		options = $.extend(defaults, options || {});
		
		var windowHeight = $(window).height();
		var windowWidth = $(window).width();
		
		var shadeHeight = 100;
		var shadeWidth = 100;
		
		if(options.shadeModel == "process"){
			shadeHeight = 20;
			shadeWidth = 166;
		}
		
		var shadeLocationX = (windowWidth-shadeWidth)/2;
		var shadeLocationY = (windowHeight-shadeHeight)/2;
		
		var div = $("<div id='shadeModelShowDiv'></div>").height(windowHeight+"px").width(windowWidth+"px").css("position","absolute").css("left","0px").css("top","0px").css("z-index","9999999");
		var contantDiv = $("<div></div>").height(shadeHeight+"px").width(shadeWidth+"px").css("position","absolute").css("left",shadeLocationX+"px").css("top",shadeLocationY+"px");
		var shadeUrl=url("/shade?model="+options.shadeModel);
		contantDiv.load(shadeUrl);
		div.append(contantDiv);
		
		$("body").append(div);
		
		jQuery.ajax({
			cache : options.cache,
			async : options.async,
			timeout : options.timeout,
            type : options.type,
            url : options.url,
			data : options.data,
			dataType :options.dataType,
			error : function(request,textStatus,errorThrown){
				options.error(request,textStatus,errorThrown);
				if(div != null){
        			div.remove();
        		}
			},
            success : function(data) {
				if(div != null){
        			div.remove();
        		}
				options.success(data);
            }
		});
	}
	
	$.fn.ajaxFromWithShade = function(options){
		var that = $(this);
		var defaults = {    
			timeout : 60000,
			async : true,
			cache : false,
			shadeModel : "default",
			error : function(){},
			success : function(){},
			beforeSend : function(){return true;},
			complete : function(){}
		};
			  
		options = $.extend(defaults, options || {});
		
		var windowHeight = $(window).height();
		var windowWidth = $(window).width();
		
		var shadeHeight = 100;
		var shadeWidth = 100;
		
		if(options.shadeModel == "process"){
			shadeHeight = 20;
			shadeWidth = 166;
		}
		
		var shadeLocationX = (windowWidth-shadeWidth)/2;
		var shadeLocationY = (windowHeight-shadeHeight)/2;
		
		var div = $("<div id='shadeModelShowDiv'></div>").height(windowHeight+"px").width(windowWidth+"px").css("position","absolute").css("left","0px").css("top","0px").css("z-index","9999999");
		var contantDiv = $("<div></div>").height(shadeHeight+"px").width(shadeWidth+"px").css("position","absolute").css("left",shadeLocationX+"px").css("top",shadeLocationY+"px");
		var shadeUrl=url("/shade?model="+options.shadeModel);
		contantDiv.load(shadeUrl);
		div.append(contantDiv);
		
		$("body").append(div);
		
		that.ajaxSubmit({
			cache : options.cache,
			async : options.async,
			timeout : options.timeout,
            type : options.type,
            url : options.url,
			data : options.data,
			dataType :options.dataType,
			error : function(request,textStatus,errorThrown){
				options.error(request,textStatus,errorThrown);
				if(div != null){
        			div.remove();
        		}
			},
            success : function(data) {
				if(div != null){
        			div.remove();
        		}
				options.success(data);
            },
			beforeSend: function() {
				var send = options.beforeSend();
				if(!send){
					div.remove();
				}
            	return send;
            },
			complete : function(){
				options.complete();
			}
		});
	}
	
	
	
$.st_operation_btn = function(options){
		
		var defaults = {    
			    width:100,
				data:{}
			  };
			  
		options = $.extend(defaults, options || {});	  
		
		var a = $("<a name='st_table_option_mark'></a>").addClass("st-btn-tit");
		var i = $("<i></i>").css("font-size","1.5em").addClass("icon-angle-right");
		var div_0 = $("<div></div>");
		var div_1 = $("<div></div>").addClass("st-btn");
		var div_2 =  $("<div></div>").css("float","left").html("操作");
		var div_3 =  $("<div></div>").css("float","right").append(i);
		var div_4 = $("<div></div>").css("width",options.width+"px").addClass("st-btn-con");
		
		var datas = options.data;
		
		var liGroup = new Array();
		var tempGroup = null;
		var group = "-0";
		$.each(datas,function(m,n){
			
			if(group != n.group){
				tempGroup = [];
				liGroup.push(tempGroup);
				group = n.group;
			}
			var li = $("<li></li>").html(n.context);
			if(n.enable){
				li.addClass("active").attr("onclick",n.methodName+"("+n.param+")");
			}else{
				li.addClass("un-active");
			}
			tempGroup.push(li);
		});
		
		$.each(liGroup,function(k,lis){
			var ulDiv = $("<div></div>");
			var ul = $("<ul></ul>");
			if(k != (liGroup.length-1)){
				ulDiv.css("border-bottom","1px solid white").css("margin-bottom","2px");
			}
			$.each(lis,function(q,li){
				ul.append(li);
			});
			ulDiv.append(ul);
			div_4.append(ulDiv);
		
		});
		
		a.append(div_2).append(div_3);
		div_1.append(a);
		div_1.append(div_4);
		div_0.append(div_1);
		return div_0.html();
	}
	
	var operation = null;
	
	$.st_operation_btn_register = function(){
		var a = $("a[name='st_table_option_mark']");
		$.each(a,function(m,n){
			var div = $(this).parent().find("div[class='st-btn-con']");
			var i = $(this).find("div").eq(1).find("i");
			var that = $(this);
			$(this).bind("click",function(){
				var visible = div.is(":visible");
    			if(visible){
    				div.hide();
					operation = null;
					$(this).parent().css("z-index","1");
					i.removeClass("icon-angle-down");
					i.addClass("icon-angle-right");
    			}else{
    				var parentTop = that.parent().offset().top;
					var parentHeight = that.parent().height();
					var tableHeight = that.parentsUntil("div[class='datagrid-body']").last().parent().height();
					var tableHeadHeight = $("div[class='datagrid-header']").height();
					var tableLocationY = that.parentsUntil("div[class='datagrid-body']").last().parent().offset().top;;
					var divHeight = div.height();
					
					var divHeightAboutTabel = parentTop-tableLocationY;
					var divHeightLower = tableHeight - parentHeight - divHeightAboutTabel;
					if(divHeightLower < divHeight){
						var divNewLocationY = divHeightAboutTabel - divHeight + tableHeadHeight + 1;
						div.css("top", divNewLocationY+"px");
					}else{
						var divNewLocationY =  divHeightAboutTabel +tableHeadHeight + parentHeight;
						div.css("top", divNewLocationY+"px");
					}
					//div.attr("style","display:block");
				//	div.css("position","relative");
					//div.css("z-index:","9999");

					div.show();
					if(operation != null){
						operation.click();
					}
					operation = $(this);
					$(this).parent().css("z-index","100");
					i.removeClass("icon-angle-right");
					i.addClass("icon-angle-down");
    			}
			});
			
			//鼠标离开时div隐藏
			$(this).hover(function (){
			},
			function (){ 
				div.hover(function (){
					div.show();
					if(operation != null){
						operation.click();
					}
					operation = $(this);
					$(this).parent().css("z-index","100");
					i.removeClass("icon-angle-right");
					i.addClass("icon-angle-down");
				},
				function (){ 
					div.hide();
					operation = null;
					$(this).parent().css("z-index","1");
					i.removeClass("icon-angle-down");
					i.addClass("icon-angle-right");
				});
				div.hide();
				operation = null;
				$(this).parent().css("z-index","1");
				i.removeClass("icon-angle-down");
				i.addClass("icon-angle-right");
			});
		
		});
	}
	
	
	$.ajaxOpenNewWindow = function(options){
		
		var defaults = {    
			timeout : 60000,
			async : true,
			cache : false,
			shadeModel : "default",
			fileUrl : "",
			error : function(){},
			success : function(){},
			failure : function(){}
		};
		
		options = $.extend(defaults, options || {});
		
		var frame = window.open("about:blank","_blank");
		
		var windowHeight = $(window).height();
		var windowWidth = $(window).width();
		
		var shadeHeight = 100;
		var shadeWidth = 100;
		
		if(options.shadeModel == "process"){
			shadeHeight = 20;
			shadeWidth = 166;
		}
		
		var shadeLocationX = (windowWidth-shadeWidth)/2;
		var shadeLocationY = (windowHeight-shadeHeight)/2;
		
		var div = $("<div id='shadeModelShowDiv'></div>").height(windowHeight+"px").width(windowWidth+"px").css("position","absolute").css("left","0px").css("top","0px").css("z-index","9999999");
		var contantDiv = $("<div></div>").height(shadeHeight+"px").width(shadeWidth+"px").css("position","absolute").css("left",shadeLocationX+"px").css("top",shadeLocationY+"px");
		var shadeUrl=url("/shade?model="+options.shadeModel);
		contantDiv.load(shadeUrl);
		div.append(contantDiv);
		
		$("body").append(div);
		
		jQuery.ajax({
			cache : options.cache,
			async : options.async,
			timeout : options.timeout,
            type : options.type,
            url : options.url,
			data : options.data,
			dataType :options.dataType,
			error : function(request,textStatus,errorThrown){
				options.error(request,textStatus,errorThrown);
				if(div != null){
        			div.remove();
        		}
				if(frame != null){
					frame.close();
				}
			},
            success : function(data) {
				if(div != null){
        			div.remove();
        		}
				if(data.success || data.success=="success"){
					var filePath = data.filePath;
					if(filePath == null || filePath == ""){
						$.messager.alert("错误","文件路径未找到【filePath】","error");
						if(frame != null){
							frame.close();
						}
					}else{
						var url = "";
						if(options.fileUrl == null || options.fileUrl == ""){
							url = filePath;
						}else{
							url = options.fileUrl+"?filePath="+filePath;
						}
						frame.location = url;
					}
					options.success(data);
				}else{
					if(frame != null){
						frame.close();
					}
					options.failure(data);
				}
            }
		});
	}
	

})(jQuery);   
